package com.gs.dao.jdbc.impl;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.gs.bean.Case;
import com.gs.dao.CaseDAO;
import com.gs.dao.jdbc.impl.rowmapper.CaseRowMapper;
import com.liang.log.Log;
import com.liang.util.Page;

public class CaseDAOImpl extends BaseDAOImpl implements CaseDAO {
	
	private String sql;
	
	@Override
	public void addCase(Case newCase) {
		sql = "insert intot case_info(name,url,logo,describe) value(?,?,?,?)";
		int type = Types.VARCHAR;
		jdbcTemplate.update(sql, new Object[]{
				newCase.getName(),
				newCase.getUrl(),
				newCase.getLogo(),
				newCase.getDescribe()
		}, new int[]{type,type,type,type});
	}

	@Override
	public void deleteCase(Integer id) {
		sql = "delete case_info where id = ?";
		jdbcTemplate.update(sql,new Object[]{id},new int[]{Types.INTEGER});
	}

	@Override
	public Case findCase(Integer id) {
		sql = "select * from case_info where id =?";
		Case obj = null;
		obj =(Case) jdbcTemplate.queryForObject(sql,new Object[]{id}, new CaseRowMapper());
		return obj;
	}

	@Override
	public List<Case> queryAll() {
		sql = "select * from case_info";
		List<Case> list = null;
		if((list = jdbcTemplate.queryForList(sql)) == null){
			list = new ArrayList<Case>(0);
		}
		return list;
	}

	@Override
	public Page<Case> queryPage(Case searchModel, int currentPage, int pageSize) {
		Page<Case> page  = null;
		String _queryQuery = "select * from case_info";
		StringBuffer _queryWhere = new StringBuffer();
		List<Case> cases = null;
		if(null == searchModel){
			cases = jdbcTemplate.query(_queryQuery, new CaseRowMapper());
		}else{
			List<Object> _args = new ArrayList<Object>(0);
			List<Integer> _argTypeObjs = new ArrayList<Integer>(0);
			
			if(searchModel.getId() != null){
				_queryWhere.append(" id = ?");
				_args.add(searchModel.getId());
				_argTypeObjs.add(Types.INTEGER);
			}
			if(searchModel.getName() != null){
				_queryWhere.append(" name like ?");
				_args.add('%'+searchModel.getName()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(searchModel.getUrl() != null){
				_queryWhere.append(" url like ?");
				_args.add('%'+searchModel.getUrl()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(searchModel.getLogo() != null){
				_queryWhere.append(" imgPath like ?");
				_args.add('%'+searchModel.getLogo()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			
			if(searchModel.getDescribe() != null){
				_queryWhere.append(" describe like ?");
				_args.add('%'+searchModel.getDescribe()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			
			if(!_queryWhere.toString().equals("")){
				_queryWhere.insert(0, " where");
			}
			_queryQuery += _queryWhere.toString();
			
			Log.info("sql:"+_queryQuery);
			int[] _argTypes = new int[_argTypeObjs.size()];
			for(int i = 0 ;  i < _argTypeObjs.size() ; i++){
				_argTypes[i] = _argTypeObjs.get(i);
			}
			cases = jdbcTemplate.query(_queryQuery, _args.toArray(), _argTypes, new CaseRowMapper());
		}
		
		if(null == cases){
			cases = new ArrayList<Case>(0);
		}
		
		page = new Page<Case>(cases , currentPage , pageSize , cases.size());
		return page;
	}

	@Override
	public void updateCase(Case updateCase) {
		sql = "insert intot case_info(name,url,logo,describe) value(?,?,?,?)";
		int type = Types.VARCHAR;
		jdbcTemplate.update(sql, new Object[]{
				updateCase.getName(),
				updateCase.getUrl(),
				updateCase.getLogo(),
				updateCase.getDescribe()
		}, new int[]{type,type,type,type});
	}

}
